#include "gamescene.h"
#include "man/fatitem.h"
#include "man/populaceitem.h"
#include "man/armsitem.h"
#include "toolkit.h"
#include "item/flyitem.h"

const int MAN_NUM = 15;

void gamescene::initMan(const MANType& Tp)
{
    ManItem* item(NULL);
    switch(Tp)
    {
    case BAOZI_MTP:
    {
        item = new PopulaceItem(BAOZI_MTP);
        connect(item,SIGNAL(RunOut(int,ManItem*)),this,SLOT(PopulaceRunOut(int,ManItem*)));
    }
        break;
    case GIRL_MTP:
    {
        item = new PopulaceItem(GIRL_MTP);
        connect(item,SIGNAL(RunOut(int,ManItem*)),this,SLOT(PopulaceRunOut(int,ManItem*)));
    }
        break;
    case CITOU_MTP:
    {
        item = new PopulaceItem(CITOU_MTP);
        connect(item,SIGNAL(RunOut(int,ManItem*)),this,SLOT(PopulaceRunOut(int,ManItem*)));
    }
        break;
    case FAT_MTP:
    {
        item = new FatItem(FAT_MTP);
    }
        break;
    case CUP_MTP:
    {
        item = new ArmsItem(CUP_MTP);
        connect(item,SIGNAL(FireSignal(int,int)),this,SLOT(BeatFly(int,int)));
    }
        break;
    case SOLDIER_MTP:
    {
        item = new ArmsItem(SOLDIER_MTP);
        connect(item,SIGNAL(FireSignal(int,int)),this,SLOT(BeatFly(int,int)));
    }
        break;
    default:
        break;
    }

    item->setZValue(m_ManZOlder++);
    item->SetAnimaType(GO_TP);
    addItem(item);
    m_manlist.append(item);
}

void gamescene::createMan()
{
    if(m_bRun)
    {
        if(m_manlist.count() < MAN_NUM)
        {
            initManWithLevel();
        }
        else
        {
            bool ballclean = true;
            for(int i = 0; i < m_manlist.count(); ++i)
            {
                if(m_manlist[i]->isVisible())
                {
                    ballclean = false;
                    break;
                }
            }
            if(ballclean)
            {
                foreach(FlyItem* item,m_flylist)
                {
                    item->m_bRun = false;
                    item->m_manItem = NULL;
                }

                foreach(ManItem* item,m_manlist)
                {
                    if(item != NULL)
                    {
                        delete item;
                        item = NULL;
                    }
                }
                m_manlist.clear();

                foreach(FlyItem* item,m_flylist)
                {
                    item->m_bRun = true;
                }
                QTimer::singleShot(100,this,SLOT(createMan()));

            }

        }
    }
}

void gamescene::initManWithLevel()
{
    switch(ToolKit::m_curlevel)
    {
    case 0:
    {
        initMan(BAOZI_MTP);
    }
        break;
    case 1:
    {
        initMan(BAOZI_MTP);
    }
        break;
    case 2:
    {
        if(qrand() % 4 < 3)
        {
            initMan(BAOZI_MTP);
        }
        else
        {
            initMan(GIRL_MTP);
        }
    }
        break;
    case 3:
    {
        if(qrand() % 2)
        {
            initMan(BAOZI_MTP);
        }
        else
        {
            initMan(GIRL_MTP);
        }
    }
        break;
    case 4:
    {
        initMan(GIRL_MTP);
    }
        break;
    case 5:
    {
        if(qrand() % 4 < 3)
        {
            initMan(BAOZI_MTP);
        }
        else
        {
            initMan(CITOU_MTP);
        }
    }
        break;
    case 6:
    {
        if(qrand() % 2)
        {
            initMan(BAOZI_MTP);
        }
        else
        {
            initMan(CITOU_MTP);
        }
    }
        break;
    case 7:
    {
        if(qrand() % 4 < 3)
        {
            initMan(GIRL_MTP);
        }
        else
        {
            initMan(CITOU_MTP);
        }
    }
        break;
    case 8:
    {
        if(qrand() % 2)
        {
            initMan(GIRL_MTP);
        }
        else
        {
            initMan(CITOU_MTP);
        }
    }
        break;
    case 9:
    {
        initMan(CITOU_MTP);
    }
        break;
    case 10:
    {
        if(qrand() % 2)
        {
            initMan(BAOZI_MTP);
        }
        else
        {
            initMan(FAT_MTP);
        }
    }
        break;
    case 11:
    {
        if(qrand() % 4 < 3)
        {
            initMan(FAT_MTP);
        }
        else
        {
            initMan(BAOZI_MTP);
        }
    }
        break;
    case 12:
    {
        if(qrand() % 2)
        {
            initMan(GIRL_MTP);
        }
        else
        {
            initMan(FAT_MTP);
        }
    }
        break;
    case 13:
    {
        if(qrand() % 4 < 3)
        {
            initMan(FAT_MTP);
        }
        else
        {
            initMan(GIRL_MTP);
        }
    }
        break;
    case 14:
    {
        if(qrand() % 2)
        {
            initMan(CITOU_MTP);
        }
        else
        {
            initMan(FAT_MTP);
        }
    }
        break;
    case 15:
    {
        if(qrand() % 5 > 2)
        {
            initMan(FAT_MTP);
        }
        else
            initMan(CUP_MTP);
    }
        break;
    case 16:
    {
        if(qrand() % 2)
        {
            initMan(GIRL_MTP);
        }
        else
        {
            initMan(CUP_MTP);
        }
    }
        break;
    case 17:
    {
        if(qrand() % 2)
        {
            initMan(CITOU_MTP);
        }
        else
        {
            initMan(SOLDIER_MTP);
        }
    }
        break;
    case 18:
    {
        if(qrand() % 2)
        {
            initMan(CUP_MTP);
        }
        else
        {
            initMan(SOLDIER_MTP);
        }
    }
        break;
    case 19:
    {
        if(qrand() % 5 > 2)
        {
            initMan(FAT_MTP);
        }
        else
        {
            if(qrand() % 2)
            {
                initMan(CUP_MTP);
            }
            else
            {
                initMan(SOLDIER_MTP);
            }
        }
    }
        break;
    default:
        break;
    }
}
